/**
 * 
 */
package de.cbf.cam.dsa.facet.talent.edit;

import java.text.NumberFormat;
import java.util.ArrayList;
import java.util.HashMap;
import java.util.List;
import java.util.Map;
import java.util.Map.Entry;

import org.apache.commons.logging.Log;
import org.apache.commons.logging.LogFactory;
import org.eclipse.jface.viewers.ColumnLabelProvider;

import de.cbf.cam.dsa.facet.talent.FacetTalent;
import de.cbf.cam.dsa.facet.talent.helper.BeTypeValue;
import de.cbf.cam.dsa.facet.talent.helper.editing_support.EditingSupport_BeSources_BeSource;
import de.cbf.cam.dsa.facet.talent.helper.editing_support.EditingSupport_BeSources_MathType;
import de.cbf.cam.dsa.facet.talent.helper.editing_support.EditingSupport_BeSources_Value;
import de.cbf.util.ui.controller.ControllerTableEdit;
import de.cbf.util.ui.helper.ColumnDescription;

/**
 * @author salter
 *
 */
public class ControllerEditBeSources extends ControllerTableEdit<BeTypeValue> {
	private static final Log logger = LogFactory.getLog(ControllerEditBeSources.class);	
	
	public static final String TEXT_UNKNOWN_MATH_TYPE = "ukn.";
	
	public static final String TEXT_COLUMN_SOURCE		= "Source";
	public static final String TEXT_COLUMN_MATH_TYPE	= "Type";
	public static final String TEXT_COLUMN_VALUE		= "Value";
	
	public static final int WIDTH_COLUMN_SOURCE		= 200;
	public static final int WIDTH_COLUMN_MATH_TYPE	=  50;
	public static final int WIDTH_COLUMN_VALUE		=  60;
	
	protected Map<String, Integer> 	operationsMapping;
	protected List<String>			sources;
	
	protected NumberFormat integerFormat;
	
	protected ControllerEditBeSources() {
		super(BeTypeValue.class);	
		integerFormat = NumberFormat.getIntegerInstance();
		customizeOperations();
		customizeSources();
	}

	
	protected void customizeOperations() {
		operationsMapping = new HashMap<String, Integer>();
		operationsMapping.put(FacetTalent.TEXT_MATH_TYPE_ADD,FacetTalent.MATH_TYPE_ADD);
		operationsMapping.put(FacetTalent.TEXT_MATH_TYPE_MULT,FacetTalent.MATH_TYPE_MULT);	
	}

	protected void customizeSources() {
		sources = new ArrayList<String>();
		sources.add(FacetTalent.BE_SOURCE_DEFAULT);				
		sources.add(FacetTalent.BE_SOURCE_INVENTORY);
		sources.add(FacetTalent.BE_SOURCE_ARMOR);
		sources.add(FacetTalent.BE_SOURCE_HELMET);
	}


	protected List<ColumnDescription> getColumnDescriptions() {
		List<ColumnDescription> descriptions = new ArrayList<ColumnDescription>();
		descriptions.add(provideColumnDescription_BeSource());
		descriptions.add(provideColumnDescription_MathType());
		descriptions.add(provideColumnDescription_Value());		
		return descriptions;
	}
	
	protected ColumnDescription provideColumnDescription_BeSource(){
		ColumnDescription columnDescription = new ColumnDescription();
		columnDescription.setEditable(true);
		columnDescription.setTitle(TEXT_COLUMN_SOURCE);
		columnDescription.setWidth(WIDTH_COLUMN_SOURCE);
		columnDescription.setLabelProvider(new ColumnLabelProvider(){
			public String getText(Object element) {
				if (element instanceof BeTypeValue) {
					BeTypeValue beSource = (BeTypeValue)element;
					String name = beSource.getName();					
					return name;
				}else{
					String err_msg = "entity of wrong type in getText(Name) : \n"+
							"Expected: "+BeTypeValue.class.getSimpleName()+"\n"+ 
							"Got: "+ (element == null ? "null" : element.getClass().getSimpleName());
					logger.error(err_msg);
				}
				return "error";
			}
		});
		columnDescription.setEditSupportClass(EditingSupport_BeSources_BeSource.class);		
		columnDescription.setEditingSupportCustomizeObject(sources);
		return columnDescription;
	}
	
	protected ColumnDescription provideColumnDescription_MathType(){
		ColumnDescription columnDescription = new ColumnDescription();
		columnDescription.setEditable(true);
		columnDescription.setTitle(TEXT_COLUMN_MATH_TYPE);
		columnDescription.setWidth(WIDTH_COLUMN_MATH_TYPE);
		columnDescription.setLabelProvider(new ColumnLabelProvider(){
			public String getText(Object element) {
				if (element instanceof BeTypeValue) {
					BeTypeValue beSource = (BeTypeValue)element;
					Integer mathType = beSource.getMathType();
					for (Entry<String,Integer> entry : operationsMapping.entrySet()) {
						if(entry.getValue().equals(mathType)){
							return entry.getKey();
						}
					}
					return TEXT_UNKNOWN_MATH_TYPE;
				}else{
					String err_msg = "entity of wrong type in getText(MathType) : \n"+
							"Expected: "+BeTypeValue.class.getSimpleName()+"\n"+ 
							"Got: "+ (element == null ? "null" : element.getClass().getSimpleName());
					logger.error(err_msg);
				}
				return "error";
			}
		});
		columnDescription.setEditSupportClass(EditingSupport_BeSources_MathType.class);		
		columnDescription.setEditingSupportCustomizeObject(operationsMapping);
		return columnDescription;
	}
	
	protected ColumnDescription provideColumnDescription_Value(){
		ColumnDescription columnDescription = new ColumnDescription();
		columnDescription.setEditable(true);
		columnDescription.setTitle(TEXT_COLUMN_VALUE);
		columnDescription.setWidth(WIDTH_COLUMN_VALUE);
		columnDescription.setLabelProvider(new ColumnLabelProvider(){
			public String getText(Object element) {
				if (element instanceof BeTypeValue) {
					BeTypeValue beTypeValue = (BeTypeValue)element;
					Integer value = beTypeValue.getValue();
					if(value != null){
						String valueString = integerFormat.format(value);						
						return valueString;
					}else{
						return "";
					}					
				}else{
					String err_msg = "entity of wrong type in getText(Value) : \n"+
							"Expected: "+BeTypeValue.class.getSimpleName()+"\n"+ 
							"Got: "+ (element == null ? "null" : element.getClass().getSimpleName());
					logger.error(err_msg);
				}
				return "error";
			}
		});
		columnDescription.setEditSupportClass(EditingSupport_BeSources_Value.class);
		return columnDescription;
	}
}
